//
//  NewsTypeViewController.m
//  xpkc
//
//  Created by TopSage on 15/11/10.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "NewsTypeViewController.h"
#import "MJRefresh.h"
#import "NewsTableViewCell.h"
#import "UIImageView+AFNetworking.h"
#import "NewsContentViewController.h"



#import "NewsTypeTableViewCell.h"

@interface NewsTypeViewController (){
    UITableView *tab;
    NSMutableArray *news;
    int pageNum;
}

@end

@implementation NewsTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    news = [NSMutableArray array];
    [self creatView];
}

- (void)creatView{
    tab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.frame.size.height-50) style:UITableViewStylePlain];
    tab.delegate = self;
    tab.dataSource = self;
    MJRefreshNormalHeader *header =
    [[MJRefreshNormalHeader alloc]init];
    [header setRefreshingTarget:self refreshingAction:@selector(refreshWithHeader:)];
    tab.header = header;
    MJRefreshAutoNormalFooter *footer = [[MJRefreshAutoNormalFooter alloc]init];
    [footer setRefreshingTarget:self refreshingAction:@selector(requestMoreWithFooter:)];
    tab.footer = footer;
    [self.view addSubview:tab];
    
    [self requestNewslistWithPage:0];
}

#pragma mark - tab delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return news.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsTypeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"newsType"];
    
    
    if (cell == nil) {
        cell = [[NewsTypeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"newsType"];
        
        UILabel * lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 95, SCREEN_WIDTH-11, 1)];
        lineLabel.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        lineLabel.layer.borderWidth = 1;
        
        [cell addSubview:lineLabel];
        
    }
    
    NewsModel * model = [news objectAtIndex:indexPath.row];
    
    
    
    cell.namelabel.text = model.newsTitle;
    NSString * date = [GetTime getYYMMDDWithDate:[NSDate dateWithTimeIntervalSince1970:[model.date doubleValue]]];
    cell.descLabel.text = [NSString stringWithFormat:@"时间：%@\t\t\t 点击：%@",date,model.click];
    
    return cell;
    
    //    NewsTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
    //    if (cell == nil) {
    //        cell = [[NewsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    //    }
    //    //TODO:赋值
    //    NewsModel *model = [news objectAtIndex:indexPath.row];
    //    cell.newsTitle.text = model.newsTitle;
    //    cell.newsContent.text = model.newsContent;
    //    if (model.newsImg.length >0) {
    //        cell.hadImg = YES;
    //        [cell.newsImg setImageWithURL:[NSURL URLWithString:model.newsImg] placeholderImage:[UIImage imageNamed:@""]];
    //    }else{
    //        cell.hadImg = NO;
    //    }
    //    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsModel *model = news[indexPath.row];
    
    NSString *newsid = model.newsId;
    NewsContentViewController *vc = [[NewsContentViewController alloc]init];
    vc.title = self.title;
    vc.newsid = newsid;
    vc.newsTitle = model.newsTitle;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - request
//  请求分类下列表数据
- (void)requestNewslistWithPage:(int)page{
    pageNum = page;
    NSString *module = @"news";
    NSString *interface = @"newslist";
    
    NSString *timeString = [GetTime getTime];
    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
    //logdebug(@"%@",urlString);
    
    NSDictionary *parameter = @{@"time":timeString,@"columnid":self.columnid,@"typeid":self.newstypeid,@"pagecode":[NSString stringWithFormat:@"%d",page]};
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"加载中";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    
    NSLog(@"%@",urlString);
    NSLog(@"%@",parameter);
    
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        
        
        // NSLog(@"%@",dic);
        
        NSArray *newslist = [dic valueForKey:@"info"];
        if (page == 0) {
            [news removeAllObjects];
        }
        for (NSDictionary *dic in newslist) {
            NewsModel *model = [[NewsModel alloc]init];
            model.newsTitle = [dic valueForKey:@"title"];
            model.newsContent = [dic valueForKey:@"description"];
            model.newsId = [NSString stringWithFormat:@"%@",[dic valueForKey:@"id"]];
            model.click = [NSString stringWithFormat:@"%@",dic[@"click"]];
            model.date = dic[@"pubdate"];
            [news addObject:model];
        }
        // logdebug(@"%@",newslist);
        
        [tab.header endRefreshing];
        [tab.footer endRefreshing];
        [hud hide:YES];
        [tab reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        logdebug(@"%@",error.localizedDescription);
        [tab.header endRefreshing];
        [tab.footer endRefreshing];
        [hud hide:YES];
    }];
}

- (void)refreshWithHeader:(MJRefreshHeader *)header{
    [self requestNewslistWithPage:0];
}
- (void)requestMoreWithFooter:(MJRefreshFooter *)footer{
    [self requestNewslistWithPage:pageNum+1];
}

#pragma mark - other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
